﻿Imports VSModel
Imports Cls_Common
Partial Class Control_WhatCookToDay_FormulaByFormulaType
    Inherits System.Web.UI.UserControl
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then

            If (Not Page.RouteData.Values("Id") Is Nothing) Then
                hidMenuId.Value = Page.RouteData.Values("Id")
                BindDatabyCate(1, CInt(hidPageSize.Value), Page.RouteData.Values("Id"))

            End If
          
        End If
    End Sub
    Sub BindDatabyCate(ByVal iPage As Integer, ByVal iPageSize As Integer, ByVal iFormulaTypeid As Integer)
        Using ssmdata As New VSEntities
            'Add DomainName DUC 09-09-2014
            Dim obj = (From ft In ssmdata.FormulaTypes Where ft.Domain = DomainName And ft.Id = iFormulaTypeid Select ft).FirstOrDefault
            If (Not obj Is Nothing) Then
                hplCatTitle.Text = obj.Title.ToString
                Excute_Javascript("SetPageTitle('" & sMetaTitle & " - " & hplCatTitle.Text & " | Trang " & iPage.ToString & "');", Me.Page, True)
            End If

            'Show data of cate
            'Add DomainName DUC 09-09-2014
            Dim q = (From f In ssmdata.formulae Where f.Domain = DomainName And f.FormulaTypeId = iFormulaTypeid And f.StatusId = 3 And f.PublishedDate <= Now And (f.eEffectiveDate >= Now Or f.eEffectiveDate Is Nothing) Order By f.PublishedDate Descending Select New With {f.Id, f.Title, f.Image, f.QueryString, f.Description, .QueryStringFT = f.FormulaType.QueryString, f.Viewed, f.Voted}).ToList
            '--BEGIN Thiet lap thong so paging sau khi query skip take
            hidCountOther.Value = q.Count
            If CInt(hidCountOther.Value) > 0 Then
                Create_PagerOther(hidCountOther.Value, iPage, iPageSize, 10)
            End If
            '--END Thiet lap thong so paging sau khi query skip take

            '--BEGIN paging by skip take
            Dim _resultData = q.Skip((iPage - 1) * iPageSize).Take(iPageSize).ToList
            '--END paging by skip take

            With rptContentByCat
                .DataSource = _resultData
                .DataBind()
            End With

        End Using
    End Sub
    Protected Sub rptContentByCat_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptContentByCat.ItemDataBound
        Dim p = CType(e.Item.DataItem, Object)

 

        Dim imgNews As Image = CType(e.Item.FindControl("imgNews"), Image)
        If (Not p.Image Is Nothing) Then
            imgNews.ImageUrl = p.Image.ToString.Replace("~/Uploads", "")
        End If
        imgNews.ImageAlign = ImageAlign.Middle

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.Text = p.Title
        hplTitle.NavigateUrl = "~/" & Page.RouteData.Values("MenuId") & "-" & strRewriteWhatCookToday & "/" & p.QueryStringFT.ToString & "/" & p.QueryString.ToString & "-" & p.Id.ToString & ".html"

        Dim lblDesc As Label = CType(e.Item.FindControl("lblDesc"), Label)
        lblDesc.Text = p.Description.ToString

        'Dim lblView As Label = CType(e.Item.FindControl("lblView"), Label)
        'lblView.Text = IIf(IsNothing(p.Voted) = True, 0, p.Voted)

        'Dim hplShared As HyperLink = CType(e.Item.FindControl("hplShared"), HyperLink)
        'hplShared.Text = " "
        'Dim hplComment As HyperLink = CType(e.Item.FindControl("hplComment"), HyperLink)
        'hplComment.Text = ""
        'hplComment.NavigateUrl = "~/" & Page.RouteData.Values("MenuId") & "-" & strRewriteWhatCookToday & "/" & p.QueryStringFT.ToString & "/" & p.QueryString.ToString & "-" & p.Id.ToString & ".html"

        'Dim hpllinkFbLike As LinkButton = CType(e.Item.FindControl("hpllinkFbLike"), LinkButton)
        'hpllinkFbLike.Attributes.Add("onclick", "return SetIdShared('" & p.Id & "');")
    End Sub

#Region "Cac thu tuc phan trang"
    Sub Create_PagerOther(ByVal Total_Record As Integer, ByVal Page_Index As Integer, ByVal Page_Size As Integer, ByVal Page2Show As Integer)
        Dim TotalPage As Integer = IIf((Total_Record Mod Page_Size) = 0, Total_Record / Page_Size, Total_Record \ Page_Size + 1)
        'lu lai tong so ban ghi
        hidIndexOther_page.Value = TotalPage
        'gan lai curPage de set active
        hidCurOther_Page.Value = Page_Index
        'generate ra left page
        If TotalPage > 1 And Page_Index <> 1 Then
            lnkFirstOther.Enabled = True
        Else
            lnkFirstOther.Enabled = False
        End If

        If TotalPage > 1 And Page_Index <> TotalPage Then
            lnkLastOther.Enabled = True
        Else
            lnkLastOther.Enabled = False
        End If
        Dim cPageGenerate_left As IEnumerable(Of Integer)
        If Page_Index <= Page2Show Then
            cPageGenerate_left = Enumerable.Range(1, Page_Index)
        Else
            cPageGenerate_left = Enumerable.Range(Page_Index - Page2Show, Page2Show)
        End If
        'generate ra right page
        Dim cPageGenerate_Right As IEnumerable(Of Integer)
        If Page_Index + Page2Show <= TotalPage Then
            cPageGenerate_Right = Enumerable.Range(Page_Index, Page2Show + 1)
        Else
            cPageGenerate_Right = Enumerable.Range(Page_Index, TotalPage - Page_Index + 1)
        End If
        'union 2 range va bind to Grid
        With rptPageOther
            .DataSource = cPageGenerate_left.Union(cPageGenerate_Right)
            .DataBind()
        End With
    End Sub
    Protected Sub rptPageOther_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptPageOther.ItemDataBound
        Dim lnkTitle As LinkButton
        lnkTitle = CType(e.Item.FindControl("lnkTitleOther"), LinkButton)
        'lnkTitle.CausesValidation = False
        'dang ky async ko postback
        Dim ScriptManager As System.Web.UI.ScriptManager = System.Web.UI.ScriptManager.GetCurrent(Me.Page)
        ScriptManager.RegisterAsyncPostBackControl(lnkTitle)

        If e.Item.DataItem = hidCurOther_Page.Value Then
            lnkTitle.CssClass = "active"
            lnkTitle.Text = e.Item.DataItem
        Else
            '      lnkTitle.CssClass = "pagesLink"
            lnkTitle.Text = e.Item.DataItem
        End If
        lnkTitle.ToolTip = e.Item.DataItem
    End Sub
    Protected Sub lnkTitleOther_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim lnkTile As LinkButton = CType(sender, LinkButton)
        'ham bind data - take skip data
        BindDatabyCate(CInt(lnkTile.ToolTip.ToString), CInt(hidPageSize.Value), hidMenuId.Value)
     
    End Sub
    Protected Sub lnkFirstOther_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkFirstOther.Click

        hidCurOther_Page.Value = hidCurOther_Page.Value - 1

        BindDatabyCate(CInt(hidCurOther_Page.Value), CInt(hidPageSize.Value), hidMenuId.Value)
    End Sub
    Protected Sub lnkLastOther_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkLastOther.Click
        hidCurOther_Page.Value = hidCurOther_Page.Value + 1
        BindDatabyCate(CInt(hidCurOther_Page.Value), CInt(hidPageSize.Value), hidMenuId.Value)
    End Sub
#End Region
End Class
